#include "ByteBuffer.h"

#include <algorithm>
#include <cstdlib>

ByteBuffer::ByteBuffer()
{
    m_read_ptr = 0;
}

void ByteBuffer::resize( unsigned int size )
{
    m_bytes.resize(size);
}

int ByteBuffer::set(char* buffer, unsigned int length)
{
    m_bytes.resize(length);
    memcpy(&m_bytes[0], buffer, length);
}

unsigned int ByteBuffer::length() const
{
    return m_bytes.size();
}

char* ByteBuffer::operator[](int idx)
{
    return &m_bytes[0];
}

const char* ByteBuffer::operator[](int idx) const
{
    return &m_bytes[0];
}

void ByteBuffer::rewind ( )
{
    m_read_ptr = 0;
}

unsigned int ByteBuffer::read ( void* dst, unsigned int count )
{
    
    unsigned int nbToRead = std::min(count, (unsigned int)m_bytes.size());
    memcpy(dst, &m_bytes[m_read_ptr], nbToRead);
    m_read_ptr += nbToRead;
    return nbToRead;
}
    